﻿<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>新闻资讯 </title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="format-detection" content="telephone=no">
        <link rel="stylesheet" href="./css/x-admin.css" media="all">
        <link rel="stylesheet" href="./css/backstage.css" media="all"> 
        <script src="js/jquery.min.js"></script>
		<link rel="stylesheet" href="./js/layui/css/layui.css" media="all">
		<script src="./js/layui/layui.all.js"></script>

   </head>
    <body>
    <div class="wrap">
        <div class="x-nav">
            <div class="title">
              <a><cite>新闻资讯管理</cite></a>
              <a href="addNews.html">新增资讯</a>
            </div>         
        </div>
         <div class="form">
            	<p><span>检索区:</span></p> 
            	<p><span>按新闻标题</span><input type="text" id="newstitle" ></p>
            	<p><span>按发布时间</span><input   placeholder="发布时间" id="newsdate"></p>                                                                      
             	<p><button class="btn2"   id="newssearch"> <i class="layui-icon">&#xe615;</i> 搜索 </button></p>
         </div>  
        <div class="x-body">         	   
	        <table class="layui-table" id="table">
	                <thead>
	                    <tr>                                       
	                        <th width="20%"> 发布日期 </th>
	                        <th width="40%"> 新闻资讯标题</th>
	                        <th width="20%"> 发布人</th>                      
	                        <th width="20%">操作</th>
	                    </tr>
	                </thead>
	                <tbody>
	                </tbody>
	        </table>	            
            <div class="pagelist">
	            <ul class="nav">
	            </ul>
	        </div>     
	        
        </div>
		</div>      
        <script>
        
       //日期控件
		$(function(){
		  layui.use('laydate', function(){
			 var laydate = layui.laydate;
			 laydate.render({elem: '#newsdate'});
		 }); 
		});
      
      
        $(function(){
        	//搜索新闻
         	$("#newssearch").click(function(){         	
           		showNews(1,$("#newstitle").val(),formatDate($("#newsdate").val()));         		
         	});
         	//初始化新闻列表
        	showNews(1,"",""); 
        	//$(".x-body").height($(document).height())
        });
      
        function showNews(pn,newstitle,newsdate){ 
    	    $.ajax({
                type:"GET",
                url:"../news/showallnews?pn="+pn+"&publish="+newsdate+"&title="+newstitle,
                dataType:"json",
                success:function(data){   
                	json_data=eval(data);
                    //console.log(json_data);
                    $("#table tbody").empty();
                	//新闻
                	$.each(json_data.obj.list, function(i, item) { 
                		// console.log(i+"   "+item);               	
                 		 $("#table tbody").append(
                			"<tr>"
                			+"<td>"+item.publish+"</td>"
                            +"<td ><a class='demo' href='newsDetail.html?id="+item.id+"'>"+item.title+"</a></td>"
                            +"<td>"+item.writer+"</td>"                
                            +"<td class='td-manage'>"                         
                            +"<a onclick=\"window.location.href='addNews.html?id="+item.id+"'\" href='javascript:void(0);'   class='mlbtn'>"
                            +"修改</a>"
                            +"<a onclick='newsdel("+item.id+","+pn+")' href='javascript:void(0);'  class='mlbtn'>" 
                            +"删除</a>"
                            +"</td></tr>"                   
                		 );
                	});
                	//分页
                	pageFun(json_data,newstitle,newsdate);
                }
            });
    	
    	}
        //新闻删除
        function newsdel(id,pn){   
        	if(confirm("确实要删除吗?")){ 
         	 $.ajax({
                 type:"GET",
                 url:"../news/delnews?id="+id,
                 dataType:"json",
                 success:function(data){   
                 	json_data=eval(data);
                    if (json_data.status==200){
                        //重新加载新闻
                        showNews(pn,"","");
                    	}
                    }
             });
        	};
        }
      //日期格式化
        function formatDate(date) {
        	if(date=="" || date==null){
        		return "";
        	}
		    var d = new Date(date),
		    month = '' + (d.getMonth() + 1),
		    day = '' + d.getDate(),
		    year = d.getFullYear();		 
		    if (month.length < 2) month = '0' + month;
		    if (day.length < 2) day = '0' + day;		 
		    return [year, month, day].join('-');
		}
        //分页函数
    	function pageFun(json_data,newstitle,newsdate){
    		pageinfo="";
    		//总页数
         	pagenums=parseInt(json_data.obj.pages);
    		//当前页
         	pagecurrent=parseInt(json_data.obj.pageNum); 
    		//可视页数
    		showpages=13;
    		
         	if(pagecurrent>1){
         		pageinfo+="<li><a href='javascript:void(0);' onclick=\"showNews("+(pagecurrent-1)+",'"+newstitle+"','"+newsdate+"')\"> < </a></li>";
         	}else{
         		pageinfo+="<li><a href='javascript:void(0);'> < </a></li>";
         	}     	
         	for_begin=1;
         	for_judge=1;
         	pageinf_begin="";
         	pageinf_end="";
         	if(pagenums>0){
    	     	if(pagenums<=showpages){ 
    	     		for_begin=1;
    	     		for_judge=pagenums;
    	     		pageinf_begin="";
    	     		pageinf_end="";
    	     	}
    	     	else if(pagenums>showpages && pagecurrent==1){
         		  for_begin=1;
    	     	  for_judge=showpages;
    	     	  pageinf_begin="";
    	     	  pageinf_end="<li><a>...</a></li>";     		   
         	   }
    	     	else if(pagenums<=(pagecurrent+showpages) && pagecurrent>1){
         		  for_begin=pagecurrent;
    	     	  for_judge=pagenums;
    	     	  pageinf_begin="<li><a>...</a></li>";
    	     	  pageinf_end="";
         	   }
    	     	else if(pagenums>(pagecurrent+showpages) && pagecurrent>1){
         		  for_begin=pagecurrent;
    	     	  for_judge=pagecurrent+showpages-1;
    	     	  pageinf_begin="<li><a>...</a></li>";
    	     	  pageinf_end="<li><a>...</a></li>";
         	   }
         	   
         	  pageinfo+=pageinf_begin; 
         	  for(i=for_begin;i<=for_judge;i++){
       			if(i==pagecurrent){
       				pageinfo+="<li  class='active'><a href='javascript:void(0);' onclick=\"showNews("+i+",'"+newstitle+"','"+newsdate+"')\">"+i+"</a></li>";
       			}else{
       				pageinfo+="<li><a href='javascript:void(0);' onclick=\"showNews("+i+",'"+newstitle+"','"+newsdate+"')\">"+i+"</a></li>";
       			}
       	      }  
         	  pageinfo+=pageinf_end;
         	
         	 if(pagecurrent<pagenums){
         		pageinfo+="<li><a href=\"javascript:showNews("+(pagecurrent+1)+",'"+newstitle+"','"+newsdate+"')\"> > </a></li>";
         	 }else{
         		pageinfo+="<li><a href='javascript:void(0);'> > </a></li>";
         	 } 
         	$(".pagelist ul").empty();
         	 $(".pagelist ul").append( pageinfo );
         	 }
    	}
        
        </script>  
    </body>
</html>